﻿var PrizeTypeJson;
var personJson; //机器人列表
var pageSize = 15;
$(document).ready(function () {

    //按照比赛名称进行 查询
    $.ajax({
        type: "post",
        data: { action: "GetTaskName", typeID: 2 },
        dataType: "json",
        async: false,
        url: "../../Ajax/TaskAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                PrizeTypeJson = data.json;
                $("#seleByType").append(InitSelectByType());
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });

    $.ajax({
        type: "post",
        data: { action: "GetListIsRebort", typeID: 2 },
        dataType: "json",
        async: false,
        url: "../../Ajax/UserAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                personJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });

    PageClickFunc(1);
    $("#txtIMSI").focus(function () {
        if ($(this).val() == "输入系统账号查询") {
            $(this).val("");
        }
    });

    $("#txtIMSI").blur(function () {
        if ($(this).val() == "") {
            $(this).val($(this).attr("default"));
        }

    });
    $("#btnSele").click(function () {
        InitTaskName(1)
    })

    function InitSelectRobotPerson(value) {
        var sel = "";
        $.each(personJson, function (k, v) {
            if ((value == null && k == 0) || v.ID == value)
                sel += "<option value='" + v.ID + "' selected='selected'>" + v.UserAccount + "</option>";
            else
                sel += "<option value='" + v.ID + "'>" + v.UserAccount + "</option>";
        })
        return sel;
    }

    //添加机器人
    $("#btnAdd").click(function () {
        var taskID = $("#seleByType").val();
        if (taskID == 0) {
            $.dialog.alert("请选择比赛！")
        }
        else {
            var content = "<table><tr><td>选择机器人：<select id='selRobotPer' >" + InitSelectRobotPerson() + "</select></td></tr></table>";

            $.dialog({
                title: "添加机器人",
                content: content,
                button: [
                    {
                        name: "提交",
                        callback: function () {
                            var userID = $("#selRobotPer").val() == null ? 0 : $("#selRobotPer").val()
                            alert(userID)
                            if (userID <= 0) {
                                $.dialog.alert("请选择机器人");
                            }
                            else {
                                $.ajax({
                                    type: "post",
                                    data: { action: "Add", taskID: taskID, userID: userID },
                                    dataType: "json",
                                    url: "../../Ajax/TaskUserAjax.ashx",
                                    beforeSend: function () {
                                    },
                                    success: function (data) {

                                    },
                                    complete: function () {
                                    },
                                    error: function () {
                                    }
                                });
                            }
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
                lock: true,
                drag: false
            });
        }
    })
})


//查询下拉框选择方法
function InitSelectByType() {
    var sel = "";
    sel += "<option value='0'>请选择</option>"
    $.each(PrizeTypeJson, function (k, v) {
        sel += "<option value='" + v.ID + "'>" + v.Name + "</option>";
    })
    return sel;
}
PageClickFunc = function (pageclickednumber) {
    InitTaskName(pageclickednumber);
}
function InitTaskName(pageclickednumber) {
    var sysAccount = $("#txtIMSI").val() == "输入系统账号查询" ? "" : $("#txtIMSI").val();
    var typeID = $("#seleByType").val() == 0 ? "" : $("#seleByType").val();
    $.ajax({
        type: "post",
        data: { action: "GetList", pageIndex: pageclickednumber, pageSize: pageSize, typeID: typeID, sysAccount: sysAccount },
        dataType: "json",
        url: "../../Ajax/TaskUserAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = data.json;
                var tbody = "";
                var pageCount = data.flag;
                var pos = data.pos;
                $.each(json, function (k, v) {
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblSortID'>";
                    if (pos > 0)
                        tbody += parseInt(pos) + 1;
                    else
                        tbody += v.RowNumber;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIMSI' id='" + v.ID + "'>";
                    tbody += v.IMSI;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblAccount'>";
                    tbody += v.UserAccount;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblName'>";
                    tbody += v.Name;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblGameCount'>";
                    tbody += v.GameCount;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblScore'>";
                    tbody += v.Score;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblMatchGold'>";
                    tbody += v.MatchGold;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblJoinTime' >";
                    tbody += v.JoinTime;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIsRobot' >";
                    tbody += v.IsRobot;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblState' >";
                    tbody += v.State;
                    tbody += "</label></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody);
                $("#pager").pager({ pagenumber: pageclickednumber, pagecount: Math.ceil(pageCount / pageSize), buttonClickCallback: PageClickFunc });
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}
function Update(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblIMSI]").first();
    Edit(lbl);
}
function Edit(lbl) {
    //lbl 是判断是添加还是修改  添加为null
    var content = "";
    if (lbl != null) {
        content += "<table><tr><td>分数：<input type='text' id='txtScore' value='" + $(lbl).parent().parent().find("lable[name=lblScore]").text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>比赛币：<input type='text' id='txtMatchGold' value='" + $(lbl).parent().parent().find("lable[name=lblMatchGold]").text() + "' style='width:200px;float:right' /></td></tr></table>"
    }
    else {
    }

    $.dialog({
        title: "更新比赛人员模板",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            AddMould(lbl, $("#txtScore"));
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}
function AddMould(contr, sortNum) {
    var Score = $(sortNum).parent().parent().parent().find("#txtScore").val();
    var MatchGold = $(sortNum).parent().parent().parent().find("#txtMatchGold").val();
    var id = $(contr).attr("id");
    var IMSI = $("#txtIMSI").val() == "输入IMSI查询" ? "" : $(contr).parent().parent().find("lable[name=lblIMSI]").text();
    var Name = $(contr).parent().parent().find("lable[name=lblName]").text();
    var JoinTime = $(contr).parent().parent().find("lable[name=lblJoinTime]").text();
    var IsRobot = $(contr).parent().parent().find("lable[name=lblIsRobot]").text();
    var State = $(contr).parent().parent().find("lable[name=lblState]").text();
    var gameCount = $(contr).parent().parent().find("lable[name=lblGameCount]").text();
    var data = contr == null ? {} :
              { action: "Update", id: id, Score: Score, MatchGold: MatchGold };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/TaskUserAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            $.dialog.tips("更新成功！", 1);
            if (contr == null) {
                var tbody = "<tr>";
                tbody += "<td><lable name='lblIMSI' id='" + data + "'>";
                tbody += IMSI;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblAccount'>";
                tbody += UserAccount;
                tbody += "</label></td>";
                tbody += "<td><lable name='lblName'>";
                tbody += Name;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblGameCount'>";
                tbody += gameCount;
                tbody += "</label></td>";
                tbody += "<td><lable name='lblScore'>";
                tbody += Score;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblMatchGold'>";
                tbody += MatchGold;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblJoinTime'>";
                tbody += JoinTime;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblIsRobot'>";
                tbody += IsRobot;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblState'>";
                tbody += State;
                tbody += "</lable></td>";
                tbody += "<td>";
                tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                tbody += "</td>";
                tbody += "</tr>"
                $("#tb1").append(tbody);
            }
            else {
                $(contr).parent().parent().find("lable[name=lblIMSI]").text(IMSI);
                $(contr).parent().parent().find("lable[name=lblName]").text(Name);
                $(contr).parent().parent().find("lable[name=lblScore]").text(Score);
                $(contr).parent().parent().find("lable[name=lblMatchGold]").text(MatchGold);
                $(contr).parent().parent().find("lable[name=lblJoinTime]").text(JoinTime);
                $(contr).parent().parent().find("lable[name=lblIsRobot]").text(IsRobot);
                $(contr).parent().parent().find("lable[name=lblState]").text(State);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}
function Delete(contr) {
    var id = $(contr).parent().parent().find("lable[name=lblIMSI]").first().attr("id");
    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: id },
            async: true,
            url: "../../Ajax/TaskUserAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $(contr).parent().parent().remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}
function AddMouldIMSI(sortIMSI) {
    var data = sortIMSI == null ? {} :
              { action: "Select", IMSI: sortIMSI };
    $.ajax({
        type: "post",
        data: data,
        dataType: "json",
        url: "../../Ajax/TaskUserAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = data.json;
                var tbody = "";
                $.each(json, function (k, v) {
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblIMSI' id='" + v.ID + "'>";
                    tbody += v.IMSI;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblName'>";
                    tbody += v.Name;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblGameCount'>";
                    tbody += v.GameCount;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblScore'>";
                    tbody += v.Score;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblMatchGold'>";
                    tbody += v.MatchGold;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblJoinTime' >";
                    tbody += v.JoinTime;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblIsRobot' >";
                    tbody += v.IsRobot;
                    tbody += "</label></td>";
                    tbody += "<td><lable name='lblState' >";
                    tbody += v.State;
                    tbody += "</label></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}
